package cn.getech.data.development.service.impl;

import cn.getech.data.development.model.req.search.SqlSearchLimitVO;
import cn.getech.data.development.model.req.search.SqlSearchRoleLimit;
import cn.getech.data.development.model.req.search.SqlSearchSaveReq;
import cn.getech.data.development.utils.hadoop.HadoopLogUtils;
import cn.getech.system.center.utils.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.development.mapper.BdpSqlSearchLimitMapper;
import cn.getech.data.development.entity.BdpSqlSearchLimit;
import cn.getech.data.development.service.BdpSqlSearchLimitService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (BdpSqlSearchLimit)表服务实现类
 *
 * @author makejava
 * @since 2021-09-23 13:38:10
 */
@Service("bdpSqlSearchLimitService")
public class BdpSqlSearchLimitServiceImpl extends ServiceImpl<BdpSqlSearchLimitMapper, BdpSqlSearchLimit> implements BdpSqlSearchLimitService {

    @Override
    public List<Map<Integer, String>> roleList() {
        //租户管理员返回为空，普通成员返回用户id
        Integer userId = this.baseMapper.isTenantAdmin(ShiroUtils.getTenantId(),ShiroUtils.getUserId().intValue());
        if(userId==null){
            //租户管理员
            return this.baseMapper.adminRoleList(ShiroUtils.getTenantId());
        }else {
            return this.baseMapper.roleList(ShiroUtils.getTenantId(),userId);
        }
    }

    @Override
    public Integer sqlSearchLimit() {
        Integer limit =this.baseMapper.sqlSearchLimit(ShiroUtils.getTenantId());
        if(limit==null||limit<0){
            limit=500;
        }
        return limit;
    }

    @Override
    public void saveLimit(SqlSearchSaveReq req) {
        List<SqlSearchLimitVO> list = this.baseMapper.listDate(ShiroUtils.getTenantId(),this.baseMapper.isTenantAdmin(ShiroUtils.getTenantId(),ShiroUtils.getUserId().intValue()));
        for(SqlSearchLimitVO vo:list){
            this.baseMapper.deleteById(vo.getId());
        }
        for(SqlSearchRoleLimit limit:req.getList()){
            BdpSqlSearchLimit sqlSearchLimit = this.baseMapper.selectOne(new QueryWrapper<BdpSqlSearchLimit>()
                    .eq("tenant_id",ShiroUtils.getTenantId())
                    .eq("role_id",limit.getRoleId()));
            if(sqlSearchLimit!=null){
                sqlSearchLimit.setLimit(limit.getLimit());
                sqlSearchLimit.setUpdateId(ShiroUtils.getUserId().intValue());
                sqlSearchLimit.setUpdateTime(new Date());
                this.baseMapper.updateById(sqlSearchLimit);
            }else {
                sqlSearchLimit = new BdpSqlSearchLimit();
                sqlSearchLimit.setRoleId(limit.getRoleId());
                sqlSearchLimit.setLimit(limit.getLimit());
                sqlSearchLimit.setCreateId(ShiroUtils.getUserId().intValue());
                sqlSearchLimit.setTenantId(ShiroUtils.getTenantId());
                sqlSearchLimit.setCreateTime(new Date());
                this.baseMapper.insert(sqlSearchLimit);
            }
        }
    }

    @Override
    public List<SqlSearchLimitVO> listDate() {
        return this.baseMapper.listDate(ShiroUtils.getTenantId(),this.baseMapper.isTenantAdmin(ShiroUtils.getTenantId(),ShiroUtils.getUserId().intValue()));
    }

}
